﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using Microsoft.SharePoint;
using Microsoft.Office.Server.SocialData;
using Microsoft.SharePoint.Portal.WebControls;
using Microsoft.SharePoint.Utilities;

namespace SPSocialUtil.SocialTagCloud
{
    /// <summary>
    /// 태그 클라우드 화면컨트롤을 생성합니다
    /// </summary>
    public class TagCloudControl : System.Web.UI.UserControl
    {
        private string MyTagBaseUrl;
        private string MySPsiteUrl;
        private string MyTagLinkUrl;
        SocialTerm[] sTerms;
        private long ItemMaxCount { get; set; }
        private long ItemMinCount { get; set; }

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="TagBaseUrl"></param>
        /// <param name="SPsiteUrl"></param>
        public TagCloudControl(string TagBaseUrl, string SPsiteUrl)
        {
            MyTagBaseUrl = TagBaseUrl;
            MySPsiteUrl = SPsiteUrl;
            MyTagLinkUrl = "";
        }

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="TagBaseUrl"></param>
        /// <param name="SPsiteUrl"></param>
        /// <param name="TagLinkUrl">http://server/site/page.aspx?tag={0}</param>
        public TagCloudControl(string TagBaseUrl, string SPsiteUrl, string TagLinkUrl)
        {
            MyTagBaseUrl = TagBaseUrl;
            MySPsiteUrl = SPsiteUrl;
            MyTagLinkUrl = "";
        }

        protected override void CreateChildControls()
        {
            if ((MySPsiteUrl != "") && (MyTagBaseUrl != ""))
            {
                using (SPSite site = new SPSite(MySPsiteUrl))
                {
                    SPServiceContext sContext = SPServiceContext.GetContext(site);
                    SocialTagManager sManager = new SocialTagManager(sContext);
                    sTerms = sManager.GetAllTerms(new System.Uri(MyTagBaseUrl), 50);

                    if ((sTerms != null) && (sTerms.Length > 0))
                    {


                    }

                }
            }
        }

        public override void RenderControl(System.Web.UI.HtmlTextWriter writer)
        {
            writer.Write("<link rel=\"stylesheet\" type=\"text/css\" href=\"/_layouts/1042/styles/socialdata.css\"/>");
            writer.Write("<div id=\"MyTagCloud\">");
            this.GetControlHtml(writer);
            writer.Write("</div>");

        }

        private void GetControlHtml(System.Web.UI.HtmlTextWriter writer)
        {
            this.GetHtml(writer, this.GetTagCloudItemsFromSocialTerms(this.sTerms));
        }

        private void GetHtml(System.Web.UI.HtmlTextWriter htw, TagCloudItem[] items)
        {
            if ((items != null) && (items.Length > 0))
            {
                this.ComputeItemCountLimits(items);
                htw.Write("<div id=\"tagCloudData" + this.ClientID + "\" class=\"ms-TagCloud ms-TagCloudLink\">");
                for (int i = 0; i < items.Length; i++)
                {
                    this.RenderItem(htw, items[i]);
                    if (i < (items.Length - 1))
                    {
                        htw.Write("<span class=\"spacer\"> </span>");
                    }
                }
                htw.Write("</div>");
            }
        }

        private void RenderItem(System.Web.UI.HtmlTextWriter htw, TagCloudItem item)
        {
            if (MyTagLinkUrl == "")
            {
                htw.Write("<a class=\"rank" + this.GetItemRank(item, 9).ToString(CultureInfo.InvariantCulture) + "\" href=\"#\">");
            }
            else
            {
                htw.Write("<a class=\"rank" + this.GetItemRank(item, 9).ToString(CultureInfo.InvariantCulture) + "\" href=\"" + string.Format(MyTagLinkUrl, SPHttpUtility.HtmlEncode(item.Name)) + "\">");
            }

            htw.Write(SPHttpUtility.HtmlEncode(item.Name));
            //htw.Write(SPHttpUtility.HtmlEncode("(" + item.Count.ToString(CultureInfo.CurrentCulture) + ")"));
            htw.Write("</a>");
        }


        private uint GetItemRank(TagCloudItem item, uint nMaxRank)
        {
            float itemMaxCount = this.ItemMaxCount;
            float itemMinCount = this.ItemMinCount;
            if (itemMinCount == itemMaxCount)
            {
                return (1 + (nMaxRank / 2));
            }
            uint num3 = 1 + ((uint)(((nMaxRank - 1) * (item.Count - itemMinCount)) / (itemMaxCount - itemMinCount)));
            //if (SocialDataGlobal.IsQuickTag(item.Name) && (num3 > (1 + (nMaxRank / 2))))
            //{
            //    num3 = 1 + (nMaxRank / 2);
            //}
            return num3;

        }

        private void ComputeItemCountLimits(TagCloudItem[] items)
        {
            if ((items == null) || (items.Length == 0))
            {
                this.ItemMaxCount = 0L;
                this.ItemMinCount = 0L;
            }
            else
            {
                long count = -9223372036854775808L;
                long num2 = 0x7fffffffffffffffL;
                foreach (TagCloudItem item in items)
                {
                    if (item.Count > count)
                    {
                        count = item.Count;
                    }
                    if (item.Count < num2)
                    {
                        num2 = item.Count;
                    }
                }
                this.ItemMaxCount = count;
                this.ItemMinCount = num2;
            }
        }

        protected virtual TagCloudItem[] GetTagCloudItemsFromSocialTerms(SocialTerm[] terms)
        {
            List<TagCloudItem> list = new List<TagCloudItem>();
            if (terms != null)
            {
                foreach (SocialTerm term in terms)
                {
                    list.Add(this.GetTagCloudItemFromSocialTerm(term));
                }
            }
            return list.ToArray();
        }

        protected virtual TagCloudItem GetTagCloudItemFromSocialTerm(SocialTerm term)
        {
            //return new TagCloudItem(term.Term.Name, term.Count, this.CreateLinkUrl(term));
            return new TagCloudItem(term.Term.Name, term.Count, this.CreateLinkUrl(term));
        }

        internal virtual string CreateLinkUrl(SocialTerm term)
        {
            return SPHttpUtility.NoEncode("");
        }
    }
}
